package com.beiing.tianshuai.tianshuai.huodong;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.util.LogUtils;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.beiing.tianshuai.tianshuai.widget.ShowImageWebView;
import com.bumptech.glide.Glide;

import butterknife.BindView;

/**
 * Created by magikliu on 2017/11/14.
 */

public class HuoDongPreviewActivity extends BaseActivity {

    private String cover;
    private String sponsor;
    private String avatar;
    private String title;
    private String begin_time;
    private String end_time;
    private String location;
    private String tel;
    private String praise;
    private String content;

    private WebSettings webSettings;

    @BindView(R.id.toolbar_iv_back)
    ImageView toolbarIvBack;
    @BindView(R.id.toolbar_tv_title)
    TextView toolbarTvTitle;
    @BindView(R.id.framelayout)
    FrameLayout frameLayout;
    @BindView(R.id.huodong_detail_cover)
    ImageView huodongDetailCover;
    @BindView(R.id.huodong_detail_header)
    CircleImageView huodongDetailHeader;
    @BindView(R.id.huodong_detail_sponsor)
    TextView huodongDetailSponsor;
    @BindView(R.id.activity_detail_title)
    TextView activityDetailTitle;
    @BindView(R.id.detail_time)
    TextView detailTime;
    @BindView(R.id.detail_view)
    TextView detailView;
    @BindView(R.id.detail_share)
    TextView detailShare;
    @BindView(R.id.detail_praise)
    TextView detailPraise;
    @BindView(R.id.huodong_detail_time)
    TextView huodongDetailTime;
    @BindView(R.id.huodong_detail_location)
    TextView huodongDetailLocation;
    @BindView(R.id.huodong_detail_signup_num)
    TextView huodongDetailSignupNum;
    @BindView(R.id.huodong_detail_tel)
    TextView huodongDetailTel;
    @BindView(R.id.huodong_detail_payment)
    TextView huodongDetailPayment;
    @BindView(R.id.huodong_detail_web)
    ShowImageWebView huodongDetailWeb;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_huodong_preview;
    }

    @Override
    protected void init() {
        initData();
        initView();
        initListener();
    }

    private void initData() {
        //标题部分
        toolbarTvTitle.setText("预览");
        toolbarIvBack.setVisibility(View.VISIBLE);
        //数据部分
        Intent intent = getIntent();
        cover = intent.getStringExtra("cover");
        sponsor = intent.getStringExtra("sponsor");
        title = intent.getStringExtra("title");
        begin_time = intent.getStringExtra("beginTime");
        end_time = intent.getStringExtra("endTime");
        location = intent.getStringExtra("location");
        tel = intent.getStringExtra("tel");
        praise = intent.getStringExtra("praise");
        content = getHtml(intent.getStringExtra("content1"));
        content = content
                .replaceAll("&lt;","<")
                .replaceAll("&gt;",">")
                .replaceAll("&quot;","")
                .replaceAll("&amp;","&");
    }

    private void initView(){
        WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
        int width = 0;
        if (wm != null) {
            width = wm.getDefaultDisplay().getWidth();
            float height = (float)width / (16f/9f);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) frameLayout.getLayoutParams();
            layoutParams.height = (int)height;
            frameLayout.setLayoutParams(layoutParams);
        }
        Glide.with(mContext.getApplicationContext())
                .load(HttpRequestConstant.ACTIVITY_PIC_HEAD + cover)
                .into(huodongDetailCover);
        huodongDetailSponsor.setText(sponsor);
        activityDetailTitle.setText(title);
        huodongDetailTime.setText(begin_time + " - " + end_time);
        huodongDetailLocation.setText(location);
        huodongDetailTel.setText(tel);
        huodongDetailPayment.setText(praise);

        webSettings = huodongDetailWeb.getSettings();
        huodongDetailWeb.loadDataWithBaseURL(null, content, "text/html","utf-8", null);
    }

    private void initListener(){
        toolbarIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private String getHtml(String html) {
        html = "<html> \n" +
                "<head> \n" +
                "<style type=\\\"text/css\\\"> \n" +
                "body {font-size:15px;}\n" +
                "</style> \n" +
                "</head> \n" +
                "<body>\n" +
                "<script type='text/javascript'>\n" +
                "window.onload = function(){\n" +
                "var $img = document.getElementsByTagName('img');\n" +
                "for(var p in $img){\n" +
                "$img[p].style.width = '90%'\n" +
                "$img[p].style.height ='auto'\n" +
                "$img[p].style.display ='block'\n" +
                "$img[p].style.margin ='0 auto'\n" +
                "}\n" +
                "}\n" +
                "\t\t\t\t</script>" + html + "\n" +
                "</body>\n" +
                "</html>";
        LogUtils.i(html, "HTML");
        return html;
    }

}
